<?php

declare(strict_types=1);

namespace app\admin\middleware;

use app\model\User;
use think\facade\Request;

class Auth
{
    public function handle($request, \Closure $next)
    {
        if ($request->pathinfo() === 'user/login') {
            return $next($request);
        }

        $authorization = Request::header('Authorization', 'Bearer ');

        $token = str_replace('Bearer ', '', $authorization);

        $request->user = User::where('token', $token)->with(['roles'])->find();

        return $next($request);
    }
}
